/*
 
 HLPViewController.m
 
 Coding for HLPViewController.m has used reference from the following link
 http://lievendekeyser.net/index.php?module=messagebox&action=message&msg_id=1351
 
 Author: Steven He <cha42@sfu.ca>
 
 Copyright (C) 2010 Green-Power
 
 URL: http://code.google.com/p/green-power
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
 $Id$
 */

#import "HLPViewController.h"
#import "MainMenuViewController.h"

@interface HLPView : UIView
{
	NSArray * mImages; //Image Array, all images will be loaded and stored here for use
	UIImageView * mLeftImageView;
	UIImageView * mCurrentImageView;
	UIImageView * mRightImageView;
	
	NSUInteger mCurrentImage;
	
	BOOL mSwiping; // YES if swiping should countinue
	CGFloat mSwipeStart;
}

- (id)initWithImages:(NSArray *)inImages;

@end //HLPView

#pragma mark -

@implementation HLPView

//Fundamental User gesture input will be handled here

- (UIImageView *)createImageView:(NSUInteger)inImageIndex
{
	if (inImageIndex >= [mImages count])
	{
		return nil;
	}
	
	UIImageView * result = [[UIImageView alloc] initWithImage:[mImages objectAtIndex:inImageIndex]];
	result.opaque = YES;
	result.userInteractionEnabled = YES;
	result.backgroundColor = [UIColor blackColor];
	
	result.contentMode = UIViewContentModeScaleAspectFit;
	result.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
	return result;
}



- (id)initWithImages:(NSArray *)inImages
{
	if (self = [super initWithFrame:CGRectZero])
	{
		mImages = [inImages retain];
		
		NSUInteger imageCount = [inImages count];
		if (imageCount > 0)
		{
			mCurrentImageView = [self createImageView:0];
			[self addSubview:mCurrentImageView];
			
			if (imageCount > 1)
			{
				mRightImageView = [self createImageView:1];
				[self addSubview:mRightImageView];
			}
			
		}
		
		self.opaque = YES;
		self.backgroundColor = [UIColor blackColor];
		self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
	}
	
	return self;
}

- (void)dealloc
{
	[mImages release];
	[super dealloc];
}



- (void)layoutSubviews
{
	if (mSwiping)
		return;
	
	CGSize contentSize = self.frame.size;
	mLeftImageView.frame = CGRectMake(-contentSize.width, 0.0f, contentSize.width, contentSize.height);
	mCurrentImageView.frame = CGRectMake(0.0f, 0.0f, contentSize.width, contentSize.height);
	mRightImageView.frame = CGRectMake(contentSize.width, 0.0f, contentSize.width, contentSize.height);
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	if ([touches count] != 1)
		return;
	
	mSwipeStart = [[touches anyObject] locationInView:self].x;
	mSwiping = YES;
	
	mLeftImageView.hidden = NO;
	mCurrentImageView.hidden = NO;
	mRightImageView.hidden = NO;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
	if (! mSwiping || [touches count] != 1)
		return;
	
	CGFloat swipeDistance = [[touches anyObject] locationInView:self].x - mSwipeStart;
	
	CGSize contentSize = self.frame.size;
	
	mLeftImageView.frame = CGRectMake(swipeDistance - contentSize.width, 0.0f, contentSize.width, contentSize.height);
	mCurrentImageView.frame = CGRectMake(swipeDistance, 0.0f, contentSize.width, contentSize.height);
	mRightImageView.frame = CGRectMake(swipeDistance + contentSize.width, 0.0f, contentSize.width, contentSize.height);
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
	if (! mSwiping)
		return;
	
	CGSize contentSize = self.frame.size;
	
	NSUInteger count = [mImages count];
	
	CGFloat swipeDistance = [[touches anyObject] locationInView:self].x - mSwipeStart;
	if (mCurrentImage > 0 && swipeDistance > 50.0f)
	{
		[mRightImageView removeFromSuperview];
		[mRightImageView release];
		
		mRightImageView = mCurrentImageView;
		mCurrentImageView = mLeftImageView;
		
		mCurrentImage--;
		if (mCurrentImage > 0)
		{
			mLeftImageView = [self createImageView:mCurrentImage - 1];
			mLeftImageView.hidden = YES;
			
			[self addSubview:mLeftImageView];
		}
		else
		{
			mLeftImageView = nil;
		}
	}
	else if (mCurrentImage < count - 1 && swipeDistance < -50.0f)
	{
		[mLeftImageView removeFromSuperview];
		[mLeftImageView release];
		
		mLeftImageView = mCurrentImageView;
		mCurrentImageView = mRightImageView;
		
		mCurrentImage++;
		if (mCurrentImage < count - 1)
		{
			mRightImageView = [self createImageView:mCurrentImage + 1];
			mRightImageView.hidden = YES;
			
			[self addSubview:mRightImageView];
		}
		else
		{
			mRightImageView = nil;
		}
	}
	
	[UIView beginAnimations:@"swipe" context:NULL];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
	[UIView setAnimationDuration:0.3f];
	
	mLeftImageView.frame = CGRectMake(-contentSize.width, 0.0f, contentSize.width, contentSize.height);
	mCurrentImageView.frame = CGRectMake(0.0f, 0.0f, contentSize.width, contentSize.height);
	mRightImageView.frame = CGRectMake(contentSize.width, 0.0f, contentSize.width, contentSize.height);
	
	[UIView commitAnimations];
	
	mSwiping = NO;
}



@end //HLPView


#pragma mark -

@implementation HLPViewController


- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


- (id)init{ //Help Section for Everything except Sorting Game
  if (self = [super initWithNibName:nil bundle:nil])

	{
		NSArray * images = [NSArray arrayWithObjects:[UIImage imageNamed:@"1.jpg"], [UIImage imageNamed:@"2.jpg"], [UIImage imageNamed:@"3.jpg"], [UIImage imageNamed:@"4.jpg"], [UIImage imageNamed:@"5.jpg"], nil];
		
		self.view = [[[HLPView alloc] initWithImages:images] autorelease];
	}
	
	return self;
}

- (id)initForSG{ //Help Section for Sorting Game ONLY
	if (self = [super initWithNibName:nil bundle:nil])
		
	{
		NSArray * images = [NSArray arrayWithObjects:[UIImage imageNamed:@"Can Tutorial.png"], [UIImage imageNamed:@"Clear Tutorial.png"], [UIImage imageNamed:@"Gable Tutorial.png"], [UIImage imageNamed:@"Glass Tutorial.png"], [UIImage imageNamed:@"Other Tutorial.png"], [UIImage imageNamed:@"Pouch Tutorial.png"], [UIImage imageNamed:@"Tetra Tutorial.png"], nil];
		
		self.view = [[[HLPView alloc] initWithImages:images] autorelease];
	}
	
	return self;
}

- (IBAction)toggleNavigationBar:(id)sender {
    //Check the current state of the navigation bar...
    BOOL navBarState = [self.navigationController isNavigationBarHidden];
    //Set the navigationBarHidden to the opposite of the current state.
    [self.navigationController setNavigationBarHidden:!navBarState animated:YES];

}

- (void)viewWillAppear:(BOOL)animated {
	[super viewDidAppear:animated];
	[self.navigationController setNavigationBarHidden:NO animated:YES];
	self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
	[self setTitle:@"Help"];
} 

- (void)viewWillDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
	self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
	[self.navigationController setNavigationBarHidden:YES animated:YES];

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return interfaceOrientation=UIInterfaceOrientationLandscapeLeft;
}



@end
